/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package grest;
import grest.model.Configu;
import grest.model.DataIO;
import grest.model.StoredProcedure;
import grest.view.*;
import java.awt.BorderLayout;
import java.io.*;
import java.util.Calendar;
import java.sql.Date;
import java.sql.SQLException;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author santiago
 */
public class GRest {
    private static Date pCancReservas;
    private static Date pMuestraReservas;
    private static String[] arg;
    /**
     * @param args the command line arguments
     */
    public static  void createFormConfig(){
        JFrame form = new JFrame("g-Rest");
        form.setLayout(new BorderLayout(5,5));
        form.setSize(323, 340);
        form.setVisible(true);
        VistaConfig vista = new VistaConfig(form);
        if(isUserMozo()){
            vista.getControls("txtAnticipa").setEnabled(false);
            vista.getControls("txtIntervalo").setEnabled(false);
        }else{
            //bajaReservas(getDiasCancelReserv());
        }
        form.add(vista);
        form.setVisible(true); 
        form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    }
    public static void main(String[] args) {
        //Inicializo el form principal
        arg=args;
        Configu c = new Configu();
         try
         {
            try{
                FileInputStream fileIn = new FileInputStream("configuracion.ser");
                ObjectInputStream in = new ObjectInputStream(fileIn);
                c = (Configu) in.readObject();
                in.close();
                fileIn.close();
                try{
                    DataIO.setUser(c.getUser());
                    DataIO.setUrl(c.getPath());
                    DataIO.setPass(c.getPass());
                    setDiasMostrarReserv(c.getDiasMostrarReserv());
                    setDiasCancelReserv(c.getDiasCancelReserv());
                    DataIO.init();
                    if(isUserMozo()){
                        FormMozos frmMozos = new FormMozos();
                    }else{
                        FormAdmin frmPrincipal = new FormAdmin(); 
                        bajaReservas(getDiasCancelReserv());
                    }
                    
                }catch(NullPointerException ex){
                    JOptionPane.showMessageDialog(null,"Error a leer el archivo de configuración. Ingrese una nueva");
                    createFormConfig();
                }
            }catch(IOException i)
            {
                try{
                    FileOutputStream fileOut = new FileOutputStream("configuracion.ser");
                    ObjectOutputStream out = new ObjectOutputStream(fileOut);
                    JOptionPane.showMessageDialog(null,"Configuración errónea. Ingrese una nueva");
                    createFormConfig();                    
                }catch(IOException ex){
                    JOptionPane.showMessageDialog(null,"Se ha producido un error.Detalles: " + ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                }
            }
         
         }catch(ClassNotFoundException ex){
             JOptionPane.showMessageDialog(null,"Se ha producido un error.Detalles: " + ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
         }
    }
    public static void setDiasMostrarReserv(Integer d){
        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DATE, d);
        Date fecha=new Date(cal.get(cal.YEAR)-1900, cal.get(cal.MONTH), cal.get(cal.DATE));
        GRest.pMuestraReservas=fecha;         
    }
    public static void setDiasCancelReserv(Integer d){
        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DATE, -d);
        Date fecha=new Date(cal.get(cal.YEAR)-1900, cal.get(cal.MONTH), cal.get(cal.DATE));
        GRest.pCancReservas=fecha;
    }
    public static Date getDiasMostrarReserv(){
        return GRest.pMuestraReservas;
    }
    public static Date getDiasCancelReserv(){
        return GRest.pCancReservas;
    }
    public static  boolean isUserMozo(){
        if(arg.length==0) return true;
        else if(arg[0].equals("-a")) return false;
        else return true;
    }
    public static void bajaReservas(java.sql.Date fecha){
        Calendar cal = new GregorianCalendar();
        Date fechaHoy=new Date(cal.get(cal.YEAR)-1900, cal.get(cal.MONTH), cal.get(cal.DATE));
        if(!isUserMozo()){
            if(!fecha.equals(fechaHoy)){
                try{
                    StoredProcedure p = new StoredProcedure("P_BAJAS_RESERV", false);
                    p.addParamIn(fecha);;
                    DataIO.executeEProcedure(p);
                }catch(SQLException ex){
                     JOptionPane.showMessageDialog(null,"No se han podidos dar de baja las reservas. Error" + ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);

                }
            }
        }
    }
}